import { message } from "antd";
import axios from "axios";

// 创建axios实例
const service = axios.create({
	baseURL: process.env.BASE_API, // api的base_url
});

service.defaults.withCredentials = true;

service.interceptors.request.use(
	(config) => {
		config.timeout = 1000 * 11;
		return config;
	},
	(error) => {
		// Do something with request error
		Promise.reject(error);
	},
);

service.interceptors.response.use(
	(response) => {
		//获取header请求的服务器时间，替换获取本地时间的方法
		let res = response.data;
		if (res.code === 200) {
			return res;
		}
		return Promise.reject(res.msg);
	},
	(error) => {
		message.error("网络异常");
		return Promise.reject(error);
	},
);

export default service;
